import axios from 'axios'
export default {
  namespaced: true,
  state () {
    return {
      cartList: []
    }
  },
  getters: {
    totalPrice (state) {
      return state.cartList.reduce((sum, v) => (sum += v.price * v.count), 0)
    },

    totalCount (state) {
      return state.cartList.reduce((sum, v) => (sum += v.count), 0)
    }
  },
  mutations: {
    setCartList (state, list) {
      state.cartList = list
    }
  },
  actions: {
    async reqCartList (ctx) {
      // 发请求
      const res = await axios({
        url: 'http://localhost:3000/cart'
      })
      ctx.commit('setCartList', res.data)
    }
  }
}
